package com.huawei.appmarket.service.pay.app;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.widget.Toast;
import com.huawei.appgallery.foundation.ui.framework.cardframe.bean.BaseCardBean;
import com.huawei.hms.activity.BridgeActivity;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.api.HuaweiApiClient;
import com.huawei.hms.support.api.client.PendingResult;
import com.huawei.hms.support.api.client.ResultCallback;
import com.huawei.hms.support.api.client.Status;
import com.huawei.hms.support.api.entity.pay.HwPayConstant;
import com.huawei.hms.support.api.entity.pay.ProductPayRequest;
import com.huawei.hms.support.api.pay.HuaweiPay;
import com.huawei.hms.support.api.pay.PayResult;
import com.huawei.hms.support.api.pay.ProductPayResultInfo;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.Map;
import o.ayy;
import o.ccz;
import o.dgm;
import o.dmi;
import o.dmp;
import o.dmq;
import o.Logger;
import o.ebz;
import o.ecf;
import o.ecg;
import o.ecj;
import o.eck.c;
import o.eqj;
import o.eqr;
import o.erz;
import o.esu;
import o.evd;
import o.ewm.n;

public class PayHmsEmbedActivity extends Activity implements ResultCallback<PayResult> {
    private dmi ʻ = new dmi(this) {
        final /* synthetic */ PayHmsEmbedActivity ॱ;

        {
            this.ॱ = r1;
        }

        public void run() {
            this.ॱ.ॱ(HuaweiPay.HuaweiPayApi.productPay(this.ॱ.ˏ, this.ॱ.ॱ()));
        }
    };
    private String ˊ;
    private Map<String, String> ˋ;
    private final int ˎ = 4001;
    private HuaweiApiClient ˏ;
    private BaseCardBean ॱ;
    private Context ॱॱ;
    private int ᐝ = ccz.ॱ();

    static class d implements eqr {
        private WeakReference<PayHmsEmbedActivity> ˊ;
        private int ˎ;

        d(PayHmsEmbedActivity payHmsEmbedActivity) {
            this.ˊ = new WeakReference(payHmsEmbedActivity);
            this.ˎ = dgm.ˎ((Activity) payHmsEmbedActivity);
        }

        private void ˋ() {
            PayHmsEmbedActivity payHmsEmbedActivity = this.ˊ == null ? null : (PayHmsEmbedActivity) this.ˊ.get();
            if (payHmsEmbedActivity != null) {
                payHmsEmbedActivity.finish();
            }
        }

        public void ॱ() {
            PayHmsEmbedActivity payHmsEmbedActivity = this.ˊ == null ? null : (PayHmsEmbedActivity) this.ˊ.get();
            if (payHmsEmbedActivity == null) {
                Logger.ˎ("PayHmsEmbedActivity", "onConnectSuccessed interrupt because of payActivity is null.");
                ecf.ˏ().ˎ(c.Failed);
            } else if (payHmsEmbedActivity.ˏ()) {
                Logger.ˎ("PayHmsEmbedActivity", "checkHmsBranchValid true，start check pay params and pay environment");
                payHmsEmbedActivity.ˋ();
            } else {
                ecf.ˏ().ˎ();
                ecf.ˏ().ॱ(3, this.ˎ);
                ecf.ˏ().ˎ(c.Failed);
                Toast.makeText(payHmsEmbedActivity, n.ʻﹳ, 0).show();
                Logger.ˎ("PayHmsEmbedActivity", "startPayClient interrupt beacause of checkHmsBranchValid false,pay process interrupt");
                ˋ();
            }
        }

        public void ॱ(int i) {
            Logger.ˋ("PayHmsEmbedActivity", "HwPayClient onConnectFailed，rtnCode:" + i);
            ecf.ˏ().ˎ();
            ecf.ˏ().ॱ(3, this.ˎ);
            ecf.ˏ().ˎ(c.Failed);
            ˋ();
        }

        public void ˋ(int i) {
            if (Logger.ˎ()) {
                Logger.ˏ("PayHmsEmbedActivity", "HwPayClient onResolveError，support onResolveError:" + i);
            }
            if (this.ˊ == null) {
                Activity activity = null;
            } else {
                PayHmsEmbedActivity payHmsEmbedActivity = (PayHmsEmbedActivity) this.ˊ.get();
            }
            if (activity != null) {
                HuaweiApiAvailability.getInstance().resolveError(activity, i, 1000);
                return;
            }
            Logger.ˋ("PayHmsEmbedActivity", "HwPayClient onResolveError Interrupted,payActivity is null");
            ecf.ˏ().ˎ(c.Failed);
        }
    }

    public /* synthetic */ void onResult(Object obj) {
        ˎ((PayResult) obj);
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        ayy.ˋ().ˎ(getWindow());
        evd.ˏ(getWindow());
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        if (getIntent() == null) {
            Logger.ˎ("PayHmsEmbedActivity", "validCheckIntent Failed.");
            ecf.ˏ().ˎ();
            finish();
            return;
        }
        SafeIntent safeIntent = new SafeIntent(getIntent());
        this.ॱॱ = getApplicationContext();
        Serializable serializableExtra = safeIntent.getSerializableExtra("pay_param");
        Serializable serializableExtra2 = safeIntent.getSerializableExtra("pay_bean");
        if ((serializableExtra instanceof ecg) && (serializableExtra2 instanceof BaseCardBean)) {
            this.ᐝ = dgm.ˎ((Activity) this);
            this.ˋ = ((ecg) serializableExtra).ॱ();
            this.ˊ = safeIntent.getStringExtra("pay_publickey");
            this.ॱ = (BaseCardBean) serializableExtra2;
            ˎ();
            return;
        }
        Logger.ˎ("PayHmsEmbedActivity", "serializableMap of bundle is null");
        ecf.ˏ().ˎ();
        finish();
    }

    private void ˎ() {
        ecf.ˏ().ˋ();
        this.ˏ = eqj.ˋ(new d(this));
        Logger.ˎ("PayHmsEmbedActivity", "startPayHms , " + (this.ˏ != null ? "hwpayClient not null,will startPayClient" : "hwpayClient is null"));
        if (ˏ()) {
            Logger.ˎ("PayHmsEmbedActivity", "checkHmsBranchValid true，start check pay params and pay environment");
            ˋ();
            return;
        }
        Logger.ˎ("PayHmsEmbedActivity", "startPayClient interrupt beacause of checkHmsBranchValid false,but hwpayClient is connecting asynchronous.");
    }

    private boolean ˏ() {
        if (this.ˋ == null || this.ॱ == null) {
            Logger.ˎ("PayHmsEmbedActivity", "startPayClient Failed，Reason：requstParams is null Or app bean is null");
            finish();
            return false;
        } else if (this.ˏ == null) {
            Logger.ˎ("PayHmsEmbedActivity", "startPayClient Failed，Rease：hwpayClient is null");
            finish();
            return false;
        } else if (this.ˏ.isConnected()) {
            return true;
        } else {
            Logger.ˎ("PayHmsEmbedActivity", "startPayClient Failed，Rease：hwpayClient not connected , wait hwpayClient connected ");
            return false;
        }
    }

    private void ˋ() {
        ecf.ˏ().ˋ();
        dmq.ˏ.ˊ(dmp.CONCURRENT, this.ʻ);
    }

    private void ॱ(PendingResult<PayResult> pendingResult) {
        pendingResult.setResultCallback(this);
    }

    private ProductPayRequest ॱ() {
        int parseInt;
        ProductPayRequest productPayRequest = new ProductPayRequest();
        productPayRequest.productNo = (String) this.ˋ.get(HwPayConstant.KEY_PRODUCT_NO);
        productPayRequest.merchantId = (String) this.ˋ.get(HwPayConstant.KEY_MERCHANTID);
        productPayRequest.applicationID = (String) this.ˋ.get(HwPayConstant.KEY_APPLICATIONID);
        productPayRequest.requestId = (String) this.ˋ.get(HwPayConstant.KEY_REQUESTID);
        try {
            parseInt = Integer.parseInt((String) this.ˋ.get(HwPayConstant.KEY_SDKCHANNEL));
        } catch (NumberFormatException e) {
            Logger.ˎ("PayHmsEmbedActivity", "NumberFormatException sdkChannel，error:" + e.toString());
            parseInt = 1;
        }
        productPayRequest.sdkChannel = parseInt;
        productPayRequest.urlVer = (String) this.ˋ.get(HwPayConstant.KEY_URLVER);
        productPayRequest.sign = (String) this.ˋ.get(HwPayConstant.KEY_SIGN);
        productPayRequest.merchantName = (String) this.ˋ.get(HwPayConstant.KEY_MERCHANTNAME);
        if (this.ˋ.containsKey(HwPayConstant.KEY_SERVICECATALOG)) {
            productPayRequest.serviceCatalog = (String) this.ˋ.get(HwPayConstant.KEY_SERVICECATALOG);
        } else {
            productPayRequest.serviceCatalog = "X5";
        }
        if (this.ˋ.containsKey("cdcChannel")) {
            productPayRequest.extReserved = (String) this.ˋ.get("cdcChannel");
        }
        return productPayRequest;
    }

    public void ˎ(PayResult payResult) {
        Status status = payResult.getStatus();
        Logger.ˎ("PayHmsEmbedActivity", "Pay Enviroment Checked Result:statusCode=" + status.getStatusCode() + ",statusMessage=" + status.getStatusMessage());
        if (status.getStatusCode() == 0) {
            ecf.ˏ().ˊ(false);
            try {
                status.startResolutionForResult(this, 4001);
                return;
            } catch (SendIntentException e) {
                Logger.e("PayHmsEmbedActivity", "SendIntentException!");
                return;
            }
        }
        ecf.ˏ().ˎ();
        ecf.ˏ().ॱ(3, this.ᐝ);
        ecf.ˏ().ˎ(c.Failed);
        if (erz.ˏ(this.ॱॱ, "com.huawei.android.hwpay")) {
            Toast.makeText(this.ॱॱ, n.ʻﹳ, 0).show();
        }
        finish();
        Logger.ˎ("PayHmsEmbedActivity", "Pay Failed，Reason :" + status.getStatusCode());
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        Logger.ˎ("PayHmsEmbedActivity", "Pay result onActivityResult :requestCode=" + i + ",resultCode=" + i2);
        ecf.ˏ().ˎ();
        if (i == 4001) {
            if (i2 == -1) {
                ProductPayResultInfo productPayResultFromIntent = HuaweiPay.HuaweiPayApi.getProductPayResultFromIntent(intent);
                if (productPayResultFromIntent != null) {
                    ˎ(productPayResultFromIntent);
                } else {
                    Logger.e("PayHmsEmbedActivity", "processPaySDKResult Failed, payResultInfo is null");
                    ecf.ˏ().ˎ(c.Failed);
                    esu.ˎ(HwAccountConstants.TYPE_TENCENT, this.ॱ.ॱـ(), "011");
                }
            } else {
                Logger.ˎ("PayHmsEmbedActivity", "Pay result onActivityResult Failed, canceld by user");
                ecf.ˏ().ॱ(2, this.ᐝ);
                ecf.ˏ().ˎ(c.Failed);
            }
        } else if (i == 1000) {
            if (i2 == -1) {
                int intExtra = intent.getIntExtra(BridgeActivity.EXTRA_RESULT, 0);
                if (intExtra == 0) {
                    Logger.ˎ("PayHmsEmbedActivity", "Success!");
                } else if (intExtra == 13) {
                    Logger.ˎ("PayHmsEmbedActivity", "Canceled!");
                } else if (intExtra == 8) {
                    Logger.ˎ("PayHmsEmbedActivity", "Internal error!");
                } else {
                    Logger.ˎ("PayHmsEmbedActivity", "Unknown!");
                }
            } else {
                Logger.ˎ("PayHmsEmbedActivity", "Error!");
            }
            ecf.ˏ().ॱ(3, this.ᐝ);
            ecf.ˏ().ˎ(c.Failed);
        }
        finish();
    }

    protected void ˎ(ProductPayResultInfo productPayResultInfo) {
        int returnCode = productPayResultInfo.getReturnCode();
        String errMsg = productPayResultInfo.getErrMsg();
        if (Logger.ˎ()) {
            Logger.ˏ("PayHmsEmbedActivity", "processPaySDKResult:returnCode=" + returnCode + ",errMsg=" + errMsg);
        }
        if (returnCode == 0) {
            if (ecj.ॱ(productPayResultInfo, this.ˊ)) {
                Logger.ˎ("PayHmsEmbedActivity", "HuaweiPay pay succussed , result checkSign ok, will check initDownload again.");
                ebz.ॱ().ˋ(this.ॱ.ॱـ(), this.ॱ.v_());
                ecf.ˏ().ॱ(0, this.ᐝ);
                ecf.ˏ().ˋ();
                ecf.ˏ().ॱ(this.ᐝ);
                return;
            }
            Logger.ˎ("PayHmsEmbedActivity", "HuaweiPay pay succussed , result checkSign failed.pay process interupted.");
            esu.ˎ("8", this.ॱ.ॱـ(), "012");
            ecf.ˏ().ॱ();
        } else if (30000 == returnCode) {
            Logger.ˎ("PayHmsEmbedActivity", "HuaweiPay payed Failed：User Canceled :" + errMsg);
            ecf.ˏ().ॱ(2, this.ᐝ);
            ecf.ˏ().ˎ(c.Failed);
        } else {
            Logger.ˎ("PayHmsEmbedActivity", "HuaweiPay payed Failed,Error Msg：" + errMsg);
            ecf.ˏ().ॱ(3, this.ᐝ);
            ecf.ˏ().ॱ();
            ecf.ˏ().ˎ(c.Failed);
        }
    }

    protected void onDestroy() {
        Logger.ˎ("PayHmsEmbedActivity", "PayHmsEmbedActivity onDestroy[" + (this.ॱ == null ? "bean=null" : this.ॱ.v_()) + "]");
        eqj.ˎ();
        ecf.ˏ().ˊ(false);
        super.onDestroy();
    }
}
